package com.shuangfeidasan.service.ums.impl;

import com.shuangfeidasan.mapper.ums.UmsAdminMapper;
import com.shuangfeidasan.model.ums.UmsAdmin;
import com.shuangfeidasan.service.ums.UmsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 后台用户表(UmsAdmin)表服务实现类
 * @Author JunWEI
 * @Date 2022-02-28 14:30:32
 */
@Service("umsAdminService")
public class UmsAdminServiceImpl implements UmsAdminService {
    @Resource
    private UmsAdminMapper umsAdminMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public UmsAdmin queryById(Long id) {
        return this.umsAdminMapper.queryById(id);
    }

    /**
     * 查询指定行
     *
     * @param umsAdmin 筛选条件
     * @return 查询结果
     */
    @Override
    public List<UmsAdmin> queryAll(UmsAdmin umsAdmin) {
        return this.umsAdminMapper.queryAll(umsAdmin);
    }

    /**
     * 新增数据
     *
     * @param umsAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public UmsAdmin insert(UmsAdmin umsAdmin) {
        umsAdmin.setPassword(passwordEncoder.encode(umsAdmin.getPassword()));
        this.umsAdminMapper.insert(umsAdmin);
        return umsAdmin;
    }

    /**
     * 修改数据
     *
     * @param umsAdmin 实例对象
     * @return 实例对象
     */
    @Override
    public UmsAdmin update(UmsAdmin umsAdmin) {
        umsAdmin.setPassword(passwordEncoder.encode(umsAdmin.getPassword()));
        this.umsAdminMapper.update(umsAdmin);
        return this.queryById(umsAdmin.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.umsAdminMapper.deleteById(id) > 0;
    }
}
